package servlet.form.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	String dbId = "java";
	String dbPwd = "servlet";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	//1.id/password(요청 파라미터)를 조회
	request.setCharacterEncoding("UTF-8");

	boolean resultId;
	boolean resultPwd;
	String id = request.getParameter("id");
	String password = request.getParameter("password");
	
	//2.로그인 처리(비지니스로직)
	if(id.equals(dbId))
		if(password.equals(dbPwd)){
			resultId=true;
			resultPwd=true;
			
		}else{
			resultId=true;
			resultPwd=false;
	}else{
		resultId=false;
		resultPwd=false;
	}
	
	String resultMessage = loginTest(resultId,resultPwd);

	//3. 처리결과 응답(성공||실패)
	response.setContentType("text/html;charset=UTF-8");
	PrintWriter out = response.getWriter();
	out.println("<html><body>");
	out.println("result : " + resultMessage);
	out.println("</body></html>");
	
	}
	
	public String loginTest(boolean resultId, boolean resultPwd){
		if(resultId==true && resultPwd==true) {
			return "로그인성공!";
		}else if(resultId==true&&resultPwd==false) {
			return "password가 틀렸습니다.";
		}else if(resultId==false&&resultPwd==false) {
			return "Id가 틀렸습니다.";
		}
		return null;
		
		
	
	}

}
